<?php
/*
	author: dkloz
	All the information thath the user input are in 'user'
	Returns DB_OK if registration is ok
	Else returns error number
	STATUS: NEED to DEBUg the MAIL / activation code
*/

function BL_registerUser($user)
{
	$error = DB_registerUser($user);
	if( $error != DB_OK){

		if( DB_usernameExists($user->getUsername()) ) {
			//echo "username ". $user->getUsername() . "   exists <br>";
			//die;
			UI_registerError('msg_error', "Username Exists");
			//BL_registerError( BL_USRNAME_EXISTS);
		}

		if( DB_emailExists($user->getEmail())) {
			UI_registerError('msg_error', "Email Exists");
			//BL_registerError(BL_EMAIL_EXISTS);
		}
		
		return BL_REGISTER_USER;
	}

	//send email to user for activation
	return BL_send_activation_email($user);
}


function BL_send_activation_email($user)
{
	$to = $user->GetEmail();
 	$subject = "uCompare Account Activation";
 	$activation = DB_getuseractivationcode( $user->GetUsername() );
 	$body = "Hello " . $user->GetName() . "!\n\nThank you for registering with uCompare, The best tool to compare Anything!\n";
 	$body = $body . " To activate your account click on the following link or copy it to your browser.\n\n";
 	$body = $body . "http://www.ucompare.tk/UI/activation.php?username=". $user->GetUsername() ."&code=" . $activation;
 	$body = $body . "\n\n\nRegards,\n uCompare Administration";


 	if (mail($to, $subject, $body)) {
   		return BL_OK;
  	}
  	else {
   		return BL_MAIL_ERROR;
 	}
}
?>
